#include "InputManager.h"

#include <algorithm>

// GameEngine.
#include "InputListener.h"

// CoreLib.
#include "LeAssert.h"

le::InputManager::InputManager()
{

}

le::InputManager::~InputManager()
{

}

void le::InputManager::AddInputListener( le::InputListener* inputListener )
{
	LE_ASSERT(std::find(m_Listeners.begin(), m_Listeners.end(), inputListener) == m_Listeners.end(), "Double input listener registration!");
	if (std::find(m_Listeners.begin(), m_Listeners.end(), inputListener) == m_Listeners.end())
	{
		m_Listeners.push_back(inputListener);
	}
}

void le::InputManager::RemoveInputListener(le::InputListener* inputListener)
{
	LE_ASSERT(std::find(m_Listeners.begin(), m_Listeners.end(), inputListener) != m_Listeners.end(), "Trying to remove a listener that is already been removed!");
	std::vector<le::InputListener*>::iterator at = std::find(m_Listeners.begin(), m_Listeners.end(), inputListener);
	if (at != m_Listeners.end())
	{
		m_Listeners.erase(at);
	}
}

void le::InputManager::OnInputEvent( const SDL_Event& inputEvent )
{
	const size_t size = m_Listeners.size();
	for (size_t i = 0; i < size; ++i)
	{
		m_Listeners[i]->OnInputEvent(inputEvent);
	}
}
